package demo.database.sql2o.Sql2o;

import java.sql.Connection;

import javax.sql.DataSource;

import org.sql2o.Sql2o;

import com.titan.core.utils.db.DBUtils;
import com.titan.core.utils.db.DataSourceUtils;

import demo.database.hsqldb.MemoryOnlyDBManager;


/**
 *
 *
 * @author Administrator
 *
 */
public class Configuration {
    /*
    private static String ip = "177.77.77.139";
    private static String port = "1621";
    private static String sid = "saasdb1";
    private static String username = "etms3_scdp";
    private static String password = "etms3_scdp";
    */
    private static String ip = "127.0.0.1";
    private static String port = "1521";
    private static String sid = "orcl";
    private static String username = "etms3_scdp";
    private static String password = "etms3_scdp";
    
    public static void main(String[] args) {
        try {
            testConnection();
            testDataSource();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    
    private static void testDataSource() throws Exception {
        String driver = "oracle.jdbc.driver.OracleDriver";
        String url = "jdbc:oracle:thin:@"+ip+":"+port+":"+sid;
        DataSource ds = DataSourceUtils.registerDataSource("testid", driver, url, username, password, 5, 5);
        Sql2o sql2o = new Sql2o(ds);
        
        Connection conn = null;
        try {
            conn = sql2o.getDataSource().getConnection();
            String sysdate = DBUtils.getValue(conn, "select to_char(sysdate,'yyyy-MM-dd HH24:mm:ss') from dual");
            System.out.println("TestDataSource的结果："+sysdate);
        } finally {
            DBUtils.close(conn);
        }        
    }
    
    private static void testConnection() throws Exception {
        //Sql2o sql2o = new Sql2o("jdbc:mysql://localhost:3306/myDB", "myUsername", "topSecretPassword");
        Sql2o sql2o = new Sql2o("jdbc:oracle:thin:@"+ip+":"+port+":"+sid, username, password);
        Connection conn = null;
        try {
            conn = sql2o.getDataSource().getConnection();
            String sysdate = DBUtils.getValue(conn, "select to_char(sysdate,'yyyy-MM-dd HH24:mm:ss') from dual");
            System.out.println("TestConnection的结果："+sysdate);
        } finally {
            DBUtils.close(conn);
        }
    }
    
    
    public static Connection getConnection() throws Exception {
        return DBUtils.getOracleConnection(ip, port, sid, username, password, false);
    }
    public static Sql2o getSql2o() {
        DataSource ds = MemoryOnlyDBManager.getDataSource();
        Sql2o sql2o = new Sql2o(ds);
        return sql2o;
    }

}

